package com.android.fragmentdemo.leanback
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.leanback.widget.Presenter
import com.android.fragmentdemo.R
import com.hengfoxwolf.commonbase.base.databeans.SlipItem
import com.android.fragmentdemo.databinding.ItemVgvLayoutBinding

class VGVPresenter(context: Context) : Presenter() {

    private val mContext = context

    override fun onCreateViewHolder(parent: ViewGroup?): ViewHolder {
        val mBinding = DataBindingUtil.inflate<ItemVgvLayoutBinding>(LayoutInflater.from(mContext),
            R.layout.item_vgv_layout,parent,false)
        return VGVViewHolder(mBinding).also { it.mBinding = mBinding }
    }

    override fun onBindViewHolder(viewHolder: ViewHolder?, item: Any?) {
        if (viewHolder != null) {
            val mBinding : ItemVgvLayoutBinding = (viewHolder as VGVViewHolder).mBinding as ItemVgvLayoutBinding
            val dataItem = item as? SlipItem
            dataItem?.apply {
                mBinding.vgvClRoot.setBackgroundColor(mContext.resources.getColor(dataItem.color))
                mBinding.vgvTvContent.text = "${dataItem.number}"
            }
        }
    }

    override fun onUnbindViewHolder(viewHolder: ViewHolder?) {

    }

    class VGVViewHolder(binding: ViewDataBinding) : ViewHolder(binding.root) {
       lateinit var mBinding: ViewDataBinding
    }
}